/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class InsertSort {
public:
    int calcMinimalCost(vector <int> theArray);
};
VI vi;
int nlen;
int dp[51][51];

int go( int cur_indx, int pre_indx ) {
    if ( cur_indx==nlen ) {
        return vi[ pre_indx ];
    }

    int& best = dp[cur_indx][pre_indx];
    if ( best!=-1 )return best;

    best = 0;
    // can i take current element ?
    if ( vi[ cur_indx ] >= vi[ pre_indx ] ) {
        // I can take current element

        // niye
        int v1 = vi[ pre_indx ] + go( cur_indx+1, cur_indx );
        best = max(best, v1 );
        // na niye
        int v2 = go( cur_indx +1 , pre_indx );
        best =  max(best, v2 );
    }
    else {
        // I can not take current element
        int v1 = go( cur_indx+1, pre_indx );
        best = max ( best, v1 );
    }
    return best;
}
int InsertSort :: calcMinimalCost(vector <int> theArray) {
    int retval=0;
    vi.clear();
    vi = theArray;
    nlen = SZ( vi );
    SET(dp,-1);

    int sum=0;
    for (int i=0;i<nlen;++i) {
        sum+=vi[i];
        int v = go( i+1, i );
        retval = max( retval, v );
    }
    printf("RESULT %d %d\n",sum,retval);
    return (sum-retval);
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <int> p0, bool hasAnswer, int p1) {
    cout << "Test " << testNum << ": [" << "{";
    for (int i = 0; int(p0.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << p0[i];
    }
    cout << "}";
    cout << "]" << endl;
    InsertSort *obj;
    int answer;
    obj = new InsertSort();
    clock_t startTime = clock();
    answer = obj->calcMinimalCost(p0);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p1 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p1;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    vector <int> p0;
    int p1;

    {
        // ----- test 0 -----
        int t0[] = {7,1,2,3};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 6;
        all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        int t0[] = {7,1,2,5};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 7;
        all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        int t0[] = {1,8,8,12,99};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 0;
        all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        int t0[] = {8,2,6,5,1,4};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 18;
        all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
        // ------------------
    }

    {
        // ----- test 4 -----
        int t0[] = {6,4,5,3,8,2,7,2,11,2,2};
        p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
        p1 = 24;
        all_right = KawigiEdit_RunTest(4, p0, true, p1) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING


//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
